package com.xidian.zhiancloud.controller.systemset;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xidian.zhiancloud.controller.file.FileController;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.model.exam.SysFile;
import com.xidian.zhiancloud.service.file.FileService;
import com.xidian.zhiancloud.service.user.CoorDinateDetailService;
import com.xidian.zhiancloud.service.user.PositionService;
import com.xidian.zhiancloud.util.tools.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 系统设置 地图操作
 */
@RestController
@RequestMapping("/codide")
public class CoorDinateDetailController extends FileController {

    private static final Logger log = LoggerFactory.getLogger(CoorDinateDetailController.class);

    @Autowired
    CoorDinateDetailService coorDinateDetailService;

    @Autowired
    private FileService fileService;

    @Autowired
    PositionService positionService;

    /**
     * 绘制地图信息列表
     * @param positionid 关联sys_position ID
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping("/getCoorDinateDetailList")
    public ResultDTO getCoorDinateDetailList(@RequestParam(value="positionid",
            required=false,defaultValue="0") Integer positionid){
        return ResultDTO.successOf(coorDinateDetailService.
               getCoorDinateDetailList(positionid),GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加绘制边界地图
     * @param positionid 关联sys_position ID
     * @param name 地图节点名称
     * @param coordinatemsg 类型
     *        uploadpath 文件上传的路径
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping(value = "/addCoorDinateDetail",method = RequestMethod.POST)
    public ResultDTO addCoorDinateDetail
        (@RequestParam(value="positionid") Integer positionid,
        @RequestParam(value="name") String name,
        @RequestParam(value="coordinatemsg") String coordinatemsg,String uploadpath){
        Integer status = 0;
        Integer state = 0;
        coorDinateDetailService.addCoorDinateDetail
            (positionid, name, coordinatemsg,uploadpath,status);
        positionService.updatePostion(String.valueOf(positionid), status,state);
        return ResultDTO.successOf
                (coorDinateDetailService.getCoorDinateDetailList(positionid),
                GeneralResponseEnums.SUCCESS);
    }

    /**
     * 添加绘制普通地图
     * @param positionid 关联sys_position ID
     * @param name 地图节点名称
     * @param coordinatemsg 类型
     *
     * @return 返回 ResultDTO 对象
     */
    @RequestMapping(value = "/addCoorDinateDetailCircular",
            method = RequestMethod.POST)
    public ResultDTO addCoorDinateDetailCircular
        (@RequestParam(value="positionid") Integer positionid,
         @RequestParam(value="name") String name,
         @RequestParam(value="coordinatemsg") String coordinatemsg){
        String uploadpath = null;
        Integer status = 1;
        Integer state = 0;
        coorDinateDetailService.addCoorDinateDetail(
                positionid, name, coordinatemsg,uploadpath,status);
        positionService.updatePostion(String.valueOf(positionid), status,state);
        return ResultDTO.successOf
                (coorDinateDetailService.getCoorDinateDetailList(positionid),
                GeneralResponseEnums.SUCCESS);
    }

    /**
     * 上传图片
     * @param file 文件
     * @return
     */
    @RequestMapping(value = "/uploadPictures",method=RequestMethod.POST)
    public ResultDTO uploadPictures(@RequestParam("file") MultipartFile file){
        String folder = "";//上传文件所存储的文件夹
        try{
            String url = fileService.uploadFile(file, folder);
            SysFile sysFile = new SysFile();
            sysFile.setName(FileUtils.getName(url));
            sysFile.setUrl(url);
            System.out.println(sysFile);
            return ResultDTO.successOf(sysFile, GeneralResponseEnums.SUCCESS);
        }catch (Exception e){
            log.error("上传文件失败", e);
            return ResultDTO.errorOf(GeneralResponseEnums.UPLOAD_FAILED);
        }
    }

}
